package com.study.talk.ui.app.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.study.talk.ui.adapter.GroupsAdapter
import com.study.talk.ui.app.base.BaseFragment
import com.study.talk.databinding.FragmentGroupBinding


class GroupFragment : BaseFragment() {

    private val adapter: GroupsAdapter by lazy { GroupsAdapter(GroupsAdapter.CHAT) }
    private lateinit var binding: FragmentGroupBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentGroupBinding.inflate(inflater, container, false)
        binding.fragmentGroupListView.layoutManager = LinearLayoutManager(requireContext())
        binding.fragmentGroupListView.adapter = adapter
        app.groups.observe(viewLifecycleOwner) {
            if (it.isEmpty()) {
                binding.fragmentGroupListView.visibility = View.GONE
                binding.fragmentGroupEmptyView.visibility = View.VISIBLE
            } else {
                binding.fragmentGroupListView.visibility = View.VISIBLE
                binding.fragmentGroupEmptyView.visibility = View.GONE
                adapter.submitList(it)
            }
        }
        return binding.root
    }
}